#!/bin/sh

set -e

fixsyms() {
    if test "`uname -s|tr A-Z a-z`" = darwin
    then egrep -v [.]eh$ | egrep ^_ | sed s/^_//
    else cat
    fi
}

tests() {
    for f in "$@"
    do nm $f
    done | cut -d ' ' -f 3 | fixsyms | egrep ^cttest
}

gen() {
    printf '#include "internal.h"\n'
    printf '\n'

    for t in "$@"
    do printf 'void %s(void);\n' $t
    done
    printf '\n'

    printf 'T ctmain[] = {\n'
    for t in "$@"
    do printf '    {%s, "%s"},\n' $t $t
    done
    printf '    {},\n'
    printf '};\n'
}

gen `tests "$@" || (echo >&2 no tests found; exit 1)`
